package com.dhc.appbusinessfrm.business.base;

import com.dhc.appbusinessfrm.App;
import com.dhc.appbusinessfrm.cache.ACache;
import com.dhc.appbusinessfrm.cache.PreferenceUtils;
import com.dhc.appbusinessfrm.ioc.annotation.Named;
import com.dhc.appbusinessfrm.model.MsgParseEnum;
import com.dhc.appbusinessfrm.model.ResultModel;
import com.dhc.appbusinessfrm.network.DanluNetInterface;
import com.dhc.appbusinessfrm.util.CommonConstants;

import javax.inject.Inject;

import retrofit.RetrofitError;
import retrofit.client.Response;

/**
 * Created by xy on 2015/6/11.
 */
public class BaseBusinessImpl implements BaseBusiness {

    /**
     * 网络访问接口
     */
    @Inject
    public DanluNetInterface danluNetInterface;

    /**
     * 缓存管理对象
     */
    @Inject
    public ACache aCache;

    @Inject
    @Named(CommonConstants.MALL_NAME)
    public PreferenceUtils preferenceUtils;

    public BaseBusinessImpl() {
        App.getObjectGraph().inject(this);
    }

    @Override
    public BaseBusiness getBusiness() {
        return this;
    }


    /**
     * 业务级数据请求是否成功完成
     * @param resultModel
     * @return
     */
    public boolean getBusinessHttpResult(ResultModel resultModel){
        int resultCode=resultModel.getCode();
        return resultCode==CommonConstants.Http.HTTP_OK;
    }

    /**
     * 获取公共错误枚举索引
     * @param error
     * @return
     */
    public int getRetrofitErrorIndex(RetrofitError error){
        int businessErrorIndex=MsgParseEnum.none.getIndex();
        if (error.getKind() == RetrofitError.Kind.NETWORK) {
            businessErrorIndex=MsgParseEnum.net_error.getIndex();
        } else if (error.getKind() == RetrofitError.Kind.HTTP) {
            businessErrorIndex=MsgParseEnum.http_error.getIndex();
        } else if (error.getKind() == RetrofitError.Kind.CONVERSION) {
            businessErrorIndex=MsgParseEnum.net_conversion.getIndex();
        } else if (error.getKind() == RetrofitError.Kind.UNEXPECTED) {
            businessErrorIndex=MsgParseEnum.net_unexpected.getIndex();
        }
        return businessErrorIndex;
    }
}
